#include "Logger.h"

using namespace game_engine::utils;

///////////////////////////////////////////////////////////////////////////////
// Constructors and Destructors
///////////////////////////////////////////////////////////////////////////////

Logger::Logger(void)
{
}

Logger::~Logger(void)
{
}

///////////////////////////////////////////////////////////////////////////////
// Other public methods
///////////////////////////////////////////////////////////////////////////////

void Logger::configure(const string &propFilename)
{
    log4cplus::PropertyConfigurator::doConfigure(stringToWString(propFilename));
}

void Logger::setLogLevel(const ConfigType &type)
{
    log4cplus::LoggerList loggers = log4cplus::Logger::getCurrentLoggers();
    log4cplus::LoggerList::iterator it = loggers.begin();
    while(it != loggers.end()) 
    {
        switch(type)
        {
        case DEBUG_MODE:
            (*it).setLogLevel(log4cplus::TRACE_LOG_LEVEL);
            break;
        case RELEASE_MODE:
            (*it).setLogLevel(log4cplus::INFO_LOG_LEVEL);
            break;
        default:
            (*it).setLogLevel(log4cplus::NOT_SET_LOG_LEVEL);
            break;
        }
        ++it;
    }
}

void Logger::TRACE(const string &logName, const string &msg)
{
    log4cplus::Logger logger = log4cplus::Logger::getInstance(
        stringToWString(logName));
    LOG4CPLUS_TRACE(logger, stringToWString(msg));
}

void Logger::DEBUG(const string &logName, const string &msg)
{
    log4cplus::Logger logger = log4cplus::Logger::getInstance(
        stringToWString(logName));
    LOG4CPLUS_DEBUG(logger, stringToWString(msg));
}

void Logger::INFO(const string &logName, const string &msg)
{
    log4cplus::Logger logger = log4cplus::Logger::getInstance(
        stringToWString(logName));
    LOG4CPLUS_INFO(logger, stringToWString(msg));
}

void Logger::WARN(const string &logName, const string &msg)
{
    log4cplus::Logger logger = log4cplus::Logger::getInstance(
        stringToWString(logName));
    LOG4CPLUS_WARN(logger, stringToWString(msg));
}

void Logger::ERR(const string &logName, const string &msg)
{
    log4cplus::Logger logger = log4cplus::Logger::getInstance(
        stringToWString(logName));
    LOG4CPLUS_ERROR(logger, stringToWString(msg));
}

void Logger::FATAL(const string &logName, const string &msg)
{
    log4cplus::Logger logger = log4cplus::Logger::getInstance(
        stringToWString(logName));
    LOG4CPLUS_FATAL(logger, stringToWString(msg));
}

///////////////////////////////////////////////////////////////////////////////
// Private methods
///////////////////////////////////////////////////////////////////////////////

wstring Logger::stringToWString(const string& s)
{
    wstring temp(s.length(),L' ');
    copy(s.begin(), s.end(), temp.begin());
    return temp;
}

string Logger::wstringToString(const wstring& s)
{
    string temp(s.length(), ' ');
    copy(s.begin(), s.end(), temp.begin());
    return temp;
}

